#include "kdcaudiovideoeditorplugin.h"

#include <KPluginFactory>
#include <KPluginLoader>

class KDCAudioVideoEditorPlugin;

#include <KLocale>

#include <kdclib.h>
#include "kdcaudiovideodelegate.h"

#include <QHeaderView>

K_PLUGIN_FACTORY( KDCAudioVideoEditorPluginFactory, registerPlugin< KDCAudioVideoEditorPlugin >(); )
K_EXPORT_PLUGIN( KDCAudioVideoEditorPluginFactory( "KDCAudioVideoEditorPlugin" ) )

KDCAudioVideoEditorPlugin::KDCAudioVideoEditorPlugin( QObject *parent, const QVariantList &args ) : KDCPluginInterface( parent ), pInfo( KPluginInfo("kdcaudiovideoeditorplugin.desktop", "services") ), pInterfaces( NULL ), pModel( NULL ), pView( NULL )
{

}

KDCAudioVideoEditorPlugin::~KDCAudioVideoEditorPlugin()
{
	unload();
}

QString KDCAudioVideoEditorPlugin::interfaceName() const
{
	return "KDCAudioVideoEditorPlugin";
}

const KPluginInfo& KDCAudioVideoEditorPlugin::info() const
{
	return pInfo;
}

QWidget* KDCAudioVideoEditorPlugin::widget() const
{
	return pView;
}

KDCConfigPageInterface* KDCAudioVideoEditorPlugin::configPage() const
{
	return NULL;
}

KDC::Position KDCAudioVideoEditorPlugin::tabPosition() const
{
	return KDC::Bottom;
}

bool KDCAudioVideoEditorPlugin::load()
{
	if ( pView )
		return false;

	pView = new QTableView;
	pModel = new KDCAudioVideoEditorModel( pView );
	pView->setModel( pModel );
	pView->verticalHeader()->hide();
	pView->horizontalHeader()->setStretchLastSection( true );
	pView->setShowGrid( false );
	pView->verticalHeader()->setDefaultSectionSize( 20 );
	pView->horizontalHeader()->setHighlightSections( false );
	pView->setSelectionBehavior( QAbstractItemView::SelectRows );
	pView->setItemDelegate( new KDCAudioVideoDelegate( this ) );
	return true;
}

bool KDCAudioVideoEditorPlugin::unload()
{
	deletePtr( pView );
	disconnect();
	return true;
}


bool KDCAudioVideoEditorPlugin::setInterfaces( QList<KDCBaseInterface*>& aInterfaces )
{
	pInterfaces = &aInterfaces;
	return true;
}

bool KDCAudioVideoEditorPlugin::init()
{
	foreach( KDCBaseInterface *interface, *pInterfaces )
		if ( interface->interfaceName() == "KDCDiscStructurePlugin" )
		{
			connect( static_cast<KDCPluginInterface*>( interface ), SIGNAL( objectChanged( KDCObject * ) ), this, SLOT( setObject( KDCObject * ) ) );
		}
	return true;
}

KDCObject* KDCAudioVideoEditorPlugin::currentObject() const
{
	return NULL;
}

void KDCAudioVideoEditorPlugin::setObject( KDCObject *aObject )
{
	if ( aObject && aObject->object() == KDC::Movie )
		pModel->setMovie( static_cast<KDCMovie*>( aObject ) );
	else
		pModel->setMovie( NULL );
}

void KDCAudioVideoEditorPlugin::refresh()
{

}

#include "kdcaudiovideoeditorplugin.moc"
